﻿using Hotel.Models;
using IHotelBLL.ICheckInBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;

namespace Hotel.Controllers.CheckIn.Appoinement
{
    public class AppointmentController : Controller
    {
        ResultModel result = new ResultModel();
        IAppointmentBLL _appointmentBLL;
        public AppointmentController(IAppointmentBLL appointmentBLL)
        {
                _appointmentBLL = appointmentBLL;
        }
        public IActionResult GetAppointmentOrderInfo(int page,int limit)
        {
            var appointments = _appointmentBLL.GetAppointments(page, limit, out int count);
            var res = new
            {
                data = appointments,
                count,
                code = 0
            };
            return Json(res);
        }
        public IActionResult GetAppointmentOrder(int page, int limit)
        {
            var appointments = _appointmentBLL.GetAppointmentOrders(page, limit, out int count);
            var res = new
            {
                data = appointments,
                count,
                code = 0
            };
            return Json(res);
        }
        public IActionResult GetAppointmentInfo()
        { 
            var userId = HttpContext.Session.GetString("userId");
            if(userId == null)
            {
                result.Msg = "用户未登录";
                return Json(result);
            }
            var res = new
            {
                code = 0,
                data = _appointmentBLL.GetAppointment(userId)
            };
            return Json(res);
        }
        public IActionResult HotelCancelAppointment(string appointmentId,string reason)
        {
            var creator = HttpContext.Session.GetString("employeeId");
            result.IsSuccess = _appointmentBLL.HotelCancelAppointment(creator, appointmentId, reason, out string msg);
            result.Msg = msg;
            return Json(result);
        }
        public IActionResult CustomerCancelAppointment(string appointmentId,string reason)
        {
            var userId = HttpContext.Session.GetString("userId");
            result.IsSuccess = _appointmentBLL.CustomerCancelAppointment(userId, appointmentId, reason, out string msg);
            result.Msg=msg;
            return Json(result);
        }
        public IActionResult HotelAcceptAppointment(string appointmentId)
        {
            var creator = HttpContext.Session.GetString("employeeId");
            result.IsSuccess = _appointmentBLL.HotelAcceeAppointment(creator, appointmentId, out string msg);
            result.Msg = msg;
            return Json(result);
        } 
        public IActionResult Appointment(string typeid)
        {
            var userId = HttpContext.Session.GetString("userId");
            if(userId is null)
            {
                result.Msg = "请登录再预约";
                return Json(result);
            }
            DateTime checkInDate = DateTime.Now;
            result.IsSuccess = _appointmentBLL.AddAppointmentRecord(userId, checkInDate, typeid, out string msg);
            result.Msg = msg;
            return Json(result);
        }
    }
}
